let app =getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList:'',
    hiddenmodalput: true, //可以通过hidden是否掩藏弹出框的属性，来指定那个弹出框
    plan_id:'',
    pwd: '',//支付密码
    tapIndex: '',//支付方式
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that =this;
    var id =options.id;
    // console.log(id)
    wx.request({
      url: app.data.url +'/member_plan_show/'+id,
      header:app.getHeader(),
      success:function(res){
        console.log(res.data)
        that.setData({
          dataList:res.data.plan,
          plan_id:id
        })
      },
    })
  },
  change: function (e) {
    var that = this;
    var pwd = e.detail.value;
    that.setData({
      pwd: pwd
    })
  },
  confirm: function () {
    var that = this;
    var pwd = that.data.pwd;
    if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/.test(pwd)) {
      wx.showModal({
        title: '提示',
        content: '密码不正确，请重新输入！',
        showCancel: false
      })
      return false
    } else {
      wx.request({
        url: app.data.url + '/member_order',
        method: "POST",
        header: app.getHeader(),
        data: {
          plan_id: that.data.plan_id,
          coupon_id: '',
          password: that.data.pwd,
          type: that.data.tapIndex
        },
        success: function (res) {
          // console.log(res.data)
          if (res.data == 1) {
            wx.showToast({
              title: '办理成功！',
            })
            that.setData({
              hiddenmodalput: true
            })
            setTimeout(function () {
              wx.switchTab({
                url: '../my/my',
                success: function (e) {
                  var page = getCurrentPages().pop();
                  if (page == undefined || page == null) return;
                  page.onLoad();
                }
              })
            }, 1000)
          } else {
            wx.showModal({
              title: '提示',
              content: '支付失败！',
              showCancel: false
            })
          }
        }
      })
    }
  },
  payment: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['微信支付', '余额支付', '企业支付'],
      success: function (res) {
        if (res.tapIndex == 1 || res.tapIndex == 2) {
          that.setData({
            hiddenmodalput: false,
            tapIndex: res.tapIndex + 1
          })
        } else {
          wx.request({
            url: app.data.url + '/member_order',
            data: {
              plan_id: that.data.plan_id,
              coupon_id: '',
              password: '',
              type: 1
            },
            method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            header: app.getHeader(),
            success: function (res) {
              var order = res.data;
              // console.log(order)
              wx.requestPayment({
                'timeStamp': order.timeStamp,
                'nonceStr': order.nonceStr,
                'package': order.package,
                'signType': order.signType,
                'paySign': order.paySign,
                success: function (res) {
                  wx.showToast({
                    title: "购买成功!",
                    duration: 1000,

                  });
                  setTimeout(function () {
                    wx.reLaunch({
                      url: '../my/my',
                      success: function (e) {
                        var page = getCurrentPages().pop();
                        if (page == undefined || page == null) return;
                        page.onLoad();
                      }
                    })
                   
                  }, 1000);

                },
                fail: function (res) {
                  // console.log("shibai")
                  // app.toast(res);
                  console.log(res)
                }
              })

            },
            fail: function () {
              // fail
              app.toast('网络异常！err:wxpay');
            }
          })
        }
      }
    })

  },
  cancel: function () {
    var that = this;
    that.setData({
      hiddenmodalput: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})